package com.plane.ticketse.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.plane.ticketse.controller.DTO.UserTicketDTO;
import com.plane.ticketse.entity.Ticket;
import com.plane.ticketse.entity.User;
import com.plane.ticketse.service.ITicketService;
import com.plane.ticketse.utils.Result;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.plane.ticketse.service.IUserTicketService;
import com.plane.ticketse.entity.UserTicket;

import org.springframework.web.bind.annotation.RestController;

/**
 * UserTicketController 前端控制器
 * @author 作者
 * @since 2023-12-25
 */
@RestController
@RequestMapping("/user_ticket")
public class UserTicketController {

    @Resource
    private IUserTicketService userTicketService;
    @Resource
    private ITicketService ticketService;

    /**
    *  新增或者更新
    */
    @PostMapping("/edit")
    public Result save(@RequestBody UserTicket userTicket) {
        return Result.success(userTicketService.saveOrUpdate(userTicket));
    }
    /**
    *  删除
    */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        UserTicket userTicket =userTicketService.getById(id);
        return Result.success(userTicketService.saveOrUpdate(userTicket));
    }
    /**
    *  查询所有
    */
    @GetMapping("/all")
    public Result findAll(@RequestParam String account) {
        List<UserTicketDTO> ticket = userTicketService.selectJoinList(UserTicketDTO.class,
                new MPJLambdaWrapper<UserTicket>()
                        .selectAll(UserTicket.class)
                        .eq("account", account)
//                       级联获取机票信息
                        .select(Ticket::getFlightInform, Ticket::getFlightNo,Ticket::getNotice)
                        .leftJoin(Ticket.class, Ticket::getFlightNo, UserTicket::getFlightNo)
        );
        return Result.success(ticket);
    }

    /**
     * 退票
     * @param userTicket
     * @return
     */
    @PostMapping("/refundTicket")
    public Result refundTicket(@RequestBody UserTicket userTicket){
        //获取购票信息
        QueryWrapper<UserTicket> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("flight_no",userTicket.getFlightNo());
        queryWrapper.eq("account",userTicket.getAccount());
        //删除购票信息
        userTicketService.remove(queryWrapper);
        //获取航班信息
        QueryWrapper<Ticket> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("flight_no",userTicket.getFlightNo());
        Ticket theTicket = ticketService.getOne(queryWrapper1);
        // 退票后机票数量+1
        theTicket.setTicketNum(theTicket.getTicketNum()+1);
        return Result.success(ticketService.saveOrUpdate(theTicket));
    }
    /**
    *  获取一个机票信息
    */
    @GetMapping("/getOne")
    public Result findOne(@RequestParam String account,@RequestParam String flightNo) {
        QueryWrapper<UserTicket> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",account);
        queryWrapper.eq("flight_no",flightNo);
        return Result.success(userTicketService.getOne(queryWrapper));
    }

    /**
     * 买票
     */
    @PostMapping("/buyTicket")
    public Result buyTicket(@RequestBody UserTicket userTicket){
        QueryWrapper<Ticket> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("flight_no",userTicket.getFlightNo());
        Ticket theticket = ticketService.getOne(queryWrapper);
        theticket.setTicketNum(theticket.getTicketNum()-1);
        ticketService.saveOrUpdate(theticket);
        if (userTicketService.saveOrUpdate(userTicket)){
            return Result.success("购买成功");
        }
        return Result.error("出现异常");
    }
}

